package com.yuan.springcloud.common.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.yuan.springcloud.common.exception.ErrorEnum;
import com.yuan.springcloud.common.exception.ParamValidException;
import com.yuan.springcloud.common.exception.ServiceException;
import com.yuan.springcloud.common.web.bean.ParamValidRs;
import com.yuan.springcloud.common.web.bean.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;

/**
 * 默认全局异常处理器
 * @author YouYuan
 * @date 2020/8/18 22:19
 */
@ControllerAdvice
public class DefaultGlobalExceptionHandler {

    /**
     * 处理参数校验异常，生成友好的提示信息给用户（包含所有校验不通过的属性与错误提示）
     * @param ex
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result resolveRuntimeException(RuntimeException ex) {
        return Result.error(ErrorEnum.RUNTIME_ERR, ex.getMessage());
    }

    /**
     * 业务异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result resolveServiceException(ServiceException ex) {
        return Result.error(ex.getData(), ex.getCode(), ex.getMessage());
    }
}
